#include "mystring.h"
#include <string.h>
#include "uart.h"
int StrLen(char* str)
{
    unsigned char i = 0;
    while (*str++)
        i++;
    return i;
}

//void StrAppend(char* origin,char* append)
//{
//   char* o = origin + StrLen(origin);
//    char* a = append;
//    while (*a)
//    {
//        *o = *a;
//        o++;
//        a++;
//    }
//    *o = 0;
//}

//void StrCopy(char* origin, char* destination)
//{
//    char* o = origin;
//    char* d = destination;
//    while (*o)
//    {
//        *d = *o;
//        d++;
//        o++;
//    }
//    *d = 0;
//}

int StrIndexOf(char* str, char* sub_str)
{
    unsigned char i = 0;
    while (*str != *sub_str) {
        str++, i++;
        if (!*str) return -1;
    }
    while (*str) {
        str++, sub_str++;
        if (!*sub_str) return i;
        if (*str != *sub_str) return -1;
    }
    return -1;
}

void StrSplitGetSubStr(char* str,char splitor, int index)
{
    unsigned char startpos = 0;
    unsigned char endpos = 0;
    unsigned char count = 0;
    unsigned char i;
    char* p_str = str;
    char* substr = splitbuff;
    while (1)
    {
        if (!*p_str) {
            endpos = endpos = p_str - str - 1;
            break;
        }
        else if (*p_str == splitor) {
            count++;
            if (count - 1 == index) {
                endpos = p_str - str - 1;
                break;
            }
            else {
                startpos = p_str - str + 1;
            }
        }
        p_str++;
    }
    for (i = startpos; i <= endpos; i++, substr++)
    {
        *substr = str[i];
    }
    *substr = 0;
}

void getLatitude(char* str)
{
    StrSplitGetSubStr(str, ',',2);
    splitbuff[4] = splitbuff[3];
    splitbuff[3] = splitbuff[2];
    splitbuff[2] = '.';
    splitbuff[5] = 0;
    strcpy( WDInfo.Latitude,splitbuff);
    StrSplitGetSubStr(str, ',', 3);
    strcat(WDInfo.Latitude, splitbuff);
}

void getHeight(char* str)
{
    StrSplitGetSubStr(str, ',',11);
    strcpy(WDInfo.Altitude,splitbuff);
}

void getLongitude(char* str)
{
    StrSplitGetSubStr(str, ',', 4);
    splitbuff[5] = splitbuff[4];
    splitbuff[4] = splitbuff[3];
    splitbuff[3] = '.';
    splitbuff[6] = 0;
    strcpy(WDInfo.Longitude,splitbuff);
    StrSplitGetSubStr(str, ',', 5);
    strcat(WDInfo.Longitude, splitbuff);
}